﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using YDWin.Base;
using YDServices;
using YDModel;
using DevExpress.Utils;
using YDCommon;

namespace YDWin.Order
{
    public partial class AddEmbryoForm : AddForm
    {
        OrderService svr = new OrderService();
        FactoryService fs = new FactoryService();
        SpecService ss = new SpecService();

        public T_Order editTu = new T_Order();
        public string state = "";
        public AddEmbryoForm()
        {
            InitializeComponent();


            //设置日期控件格式
            this.deOperationTime.Properties.DisplayFormat.FormatString = "yyyy年MM月dd日";
            this.deOperationTime.Properties.DisplayFormat.FormatType = DevExpress.Utils.FormatType.DateTime;
            this.deOperationTime.Properties.EditFormat.FormatString = "yyyy年MM月dd日";
            this.deOperationTime.Properties.EditFormat.FormatType = DevExpress.Utils.FormatType.DateTime;
            this.deOperationTime.Properties.Mask.EditMask = "yyyy年MM月dd日";
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            WaitDialogForm sdf = new WaitDialogForm("", "正在保存......");
            T_Order fact = new T_Order();
            if (!state.Equals("") && state.Equals("Edit"))
            {
                fact = editTu;
            }

            //if (txtEmbryoName.Text.Trim().Equals(""))
            //{
            //    Utils.ShowPrompt(MessageType.mtWarning, "规格名称不能为空!\n");
            //sdf.Close();
            //    return;
            //}
             
            fact.Factory_ID = (long)cboFactoryName.EditValue;
            fact.Factory_Name = cboFactoryName.Text;
            fact.OperatintTime = (DateTime)deOperationTime.EditValue;
            fact.Quantity = decimal.Parse(nudQuantity.Text);
            fact.SurplusQuantity = decimal.Parse(nudQuantity.Text);
            fact.UseQuantity = 0;
            fact.Weight = decimal.Parse(nudWeight.Text);
            fact.SumWeight = decimal.Parse(nudSumWeight.Text);
            fact.SurplusWeight= decimal.Parse(nudSumWeight.Text);
            fact.UseWeight = 0;
            fact.SerialNumber = txtSerialNumber.Text.Trim();
            fact.UnitPrice = decimal.Parse(nudUnitPrice.Text);
            fact.TotalPrice = decimal.Parse(nudTotalPrice.Text);
            fact.Spec_Name = cboSpecName.Text;
            fact.Spec_ID = (long)cboSpecName.EditValue; 
            //fact.IsSanding = cbSanding.Checked;
            if (!state.Equals("") && state.Equals("Edit"))
            {
                svr.EditItem(fact);
            }
            else
            {
                svr.AddItem(fact);
            }
      

            sdf.Close();

            if (svr.HasError)
            {
                Utils.ShowPrompt(MessageType.mtError, "规格信息保存失败：" + svr.Errors);
                svr.Errors.Clear();
                return;
            }

            this.DialogResult = DialogResult.OK;
            this.Close();

        }

        private void AddEmbryoForm_Load(object sender, EventArgs e)
        {
            //厂家名称下拉框
            var fDs = fs.getComboxList(0);
            cboFactoryName.Properties.ValueMember = "ID";   //实际要用的字段
            cboFactoryName.Properties.DisplayMember = "FactoryName";    //要显示的字段
            cboFactoryName.Properties.DataSource = fDs;
            cboFactoryName.Properties.SearchMode = DevExpress.XtraEditors.Controls.SearchMode.AutoComplete;//输入之后自动显示并选中下拉内容
 
            if (!state.Equals("") && state.Equals("Edit"))
            {
                //cboFactoryName.Text = editTu.Factory_Name ;
                //fact.Factory_ID=cboFactoryName.
                cboFactoryName.Text= editTu.Factory_Name;
                deOperationTime.EditValue= editTu.OperatintTime;
                nudQuantity.Text= editTu.Quantity.ToString();
                nudWeight.Text= editTu.Weight.ToString();
                nudSumWeight.Text = editTu.SumWeight.ToString();
                txtSerialNumber.Text = editTu.SerialNumber;
                nudUnitPrice.Text= editTu.UnitPrice.ToString();
                nudTotalPrice.Text= editTu.TotalPrice.ToString();
                cboSpecName.Text = editTu.Spec_Name;
                //cbSanding.CheckState = editTu.IsSanding == true? System.Windows.Forms.CheckState.Checked: System.Windows.Forms.CheckState.Unchecked;
            }
            else
            { 
                deOperationTime.EditValue = DateTime.Now;
            }


        }


        #region 数量录入后自动计算
        private void nudQuantity_KeyUp_1(object sender, KeyEventArgs e)
        {
            nudSumWeight.Text = (decimal.Parse(nudWeight.Text) * decimal.Parse( nudQuantity.Text)).ToString();
        }

        private void nudWeight_KeyUp_1(object sender, KeyEventArgs e)
        {
            nudSumWeight.Text = (decimal.Parse(nudWeight.Text) * decimal.Parse(nudQuantity.Text)).ToString();
        }

        private void nudSumWeight_KeyUp_1(object sender, KeyEventArgs e)
        {
            nudTotalPrice.Text =(decimal.Parse(nudUnitPrice.Text) * decimal.Parse(nudSumWeight.Text)).ToString();
        }

        private void nudUnitPrice_KeyUp_1(object sender, KeyEventArgs e)
        {
            nudTotalPrice.Text =( decimal.Parse(nudUnitPrice.Text) * decimal.Parse(nudSumWeight.Text)).ToString();
        }






        #endregion

 
        private void cboFactoryName_EditValueChanged(object sender, EventArgs e)
        {
            long factoryID = (long)cboFactoryName.EditValue;

            //规格下拉框
            var sDs = ss.getComboxList(factoryID,0); //胚布
            cboSpecName.Properties.ValueMember = "ID";   //实际要用的字段
            cboSpecName.Properties.DisplayMember = "SpecName";    //要显示的字段
            cboSpecName.Properties.DataSource = sDs;
            cboSpecName.Properties.SearchMode = DevExpress.XtraEditors.Controls.SearchMode.AutoComplete;//输入之后自动显示并选中下拉内容

        }
    }
}